package leihua.crm.workbench.service.impl;

import leihua.crm.commons.contants.Contants;
import leihua.crm.commons.utils.DateUtils;
import leihua.crm.commons.utils.UUIDUtils;
import leihua.crm.settings.domain.User;
import leihua.crm.workbench.domain.*;
import leihua.crm.workbench.mapper.CustomerMapper;
import leihua.crm.workbench.mapper.TranHistoryMapper;
import leihua.crm.workbench.mapper.TranMapper;
import leihua.crm.workbench.service.TranService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author leiHua
 * @Date 2022/10/25 23:49
 */
@Service
public class TranServiceImpl implements TranService {
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    TranMapper tranMapper;
    @Autowired
    TranHistoryMapper tranHistoryMapper;

    @Override
    public void saveCreateTran(Map<String, Object> map) {
        String customerName = (String) map.get("customerName");
        User user=(User)map.get(Contants.SESSION_USER);
        //根据名字查询该客户是否存在，如果不存在 ，需要创建一个该客户
        Customer customer = customerMapper.selectCustomerByName(customerName);
        if (customer==null){
            customer=new Customer();
            customer.setId(UUIDUtils.getUUID());
            customer.setOwner(user.getId());
            customer.setName(customerName);
            customer.setCreateBy(user.getId());
            customer.setCreateTime(DateUtils.formateDateTime(new Date()));
            customerMapper.insert(customer);
        }
        //封装一个Tran交易对象
        Tran tran = new Tran();
        tran.setId(UUIDUtils.getUUID());
        tran.setOwner((String) map.get("owner"));
        tran.setName((String) map.get("name"));
        tran.setMoney((String) map.get("money"));
        tran.setExpectedDate((String) map.get("expectedDate"));
        tran.setCustomerId(customer.getId());
        tran.setStage((String) map.get("stage"));
        tran.setType((String) map.get("type"));
        tran.setSource((String) map.get("source"));
        tran.setContactsId((String) map.get("contactsId"));
        tran.setCreateTime(DateUtils.formateDateTime(new Date()));
        tran.setCreateBy(user.getId());
        tran.setActivityId((String) map.get("activityId"));
        tran.setDescription((String) map.get("description"));
        tran.setContactSummary((String) map.get("contactSummary"));
        tran.setNextContactTime((String) map.get("nextContactTime"));
        tranMapper.insertSelective(tran);
        //新建一个交易历史
        TranHistory history = new TranHistory();
        history.setCreateBy(user.getId());
        history.setStage((String) map.get("stage"));
        history.setId(UUIDUtils.getUUID());
        history.setMoney((String) map.get("money"));
        history.setExpectedDate((String) map.get("expectedDate"));
        history.setCreateTime(DateUtils.formateDateTime(new Date()));
        history.setTranId(tran.getId());
        tranHistoryMapper.insert(history);
    }

    @Override
    public Tran queryTranForDetailByTranId(String id) {
        return tranMapper.selectTranForDetailById(id);
    }

    @Override
    public List<FunnelVO> queryCountOfTranGroupByStage() {
        return tranMapper.selectCountOfTranGroupByStage();
    }

    @Override
    public List<BarVO> queryCountOfTranGroupByActivity() {
        return tranMapper.selectCountOfTranGroupByActivity();
    }

    @Override
    public List<Tran> queryTranForPageByCondition(Map map) {
        return tranMapper.selectTranForPageByCondition(map);
    }

    @Override
    public int queryCountOfTranByCondition(Map map) {
        return tranMapper.selectCountOfTranByCondition(map);
    }

    @Override
    public Tran queryTranById(String tranId) {
        return tranMapper.selectByPrimaryKey(tranId);
    }

    @Override
    public int editTranSelective(Tran tran) {

        //新建一个交易历史
        TranHistory history = new TranHistory();
        history.setCreateBy(tran.getEditBy());
        history.setStage((tran.getStage()));
        history.setId(UUIDUtils.getUUID());
        history.setMoney(tran.getMoney());
        history.setExpectedDate(tran.getExpectedDate());
        history.setCreateTime(DateUtils.formateDateTime(new Date()));
        history.setTranId(tran.getId());
        tranHistoryMapper.insert(history);
        return tranMapper.updateByPrimaryKeySelective(tran);
    }

    @Override
    public int deleteTranByIds(String[] ids) {
        return tranMapper.deleteTranByIds(ids);
    }

    @Override
    public List<Tran> queryTranOfCustomerByCustomerId(String customerId) {
        return tranMapper.selectTranOfCustomerByCustomerId(customerId);
    }
}
